
public class SimpleLinkedList<E> {
	public class Node<E>{
		private E data;
		private Node<E> next;

		public Node(E d, Node<E> n){
			data= d;
			next=n;
		}
	}
	private Node<E> head,tail;
	private int size;
	public SimpleLinkedList(){
		head= null;
		tail= null;
		size=0;
	}
	public int Size(){
		return size;
	}
	public void AddFirst(E element){
		Node p= new Node(element, null);
		if(head ==null)
			tail= p;
		else 
			p.next= head;
		head = p;	
		size++;
	}
	public void Add(E newElement, int index){
		if(index<0||index>size){
			System.out.println("invalid value");
			System.exit(0);
		}

		if(index==0) AddFirst(newElement);
		else if(index==size)AddLast(newElement);
		else{
			Node p= new Node(newElement, null);

			Node<E>	q= head,address;
  
			for(int i=0;i<index-1;i++)
				q=q.next;
			address=q.next;
			q.next=p;
			p.next=address;
		}
	}
	public void AddLast(E element){
		Node p= new Node(element, null);
		if(tail==null){
			head= p;

		}
		else 
			tail.next=p;
		tail =p;
		size++;
	}
	public E get(int index){
		if(index<0||index>=size){
			System.out.println("invalid value");
			System.exit(0);
		}
		Node<E> q= head;
		for(int i=1;i<index;i++)
			q=q.next;	
		return q.data;
	}
	public E set(int index,E newElement ){
		if(index<0||index>=size){
			System.out.println("invalid value");
			System.exit(0);
		}
		Node<E> q= head;
		for(int i=0;i<index;i++)
			q=q.next;
		E temp= q.data;
		q.data=newElement;
		return temp;
	}
	public String toString(){
		String s="[";
		Node q=head;

		if(q==null)return "[]";
		while(q.next!=null){
			s+=q.data+",";
			q=q.next;
		}
		s+= q.data+"]";

		return s;
	}

	public E Remove(int index){
		if(index<0||index>=size){
			System.out.println("invalid value");
			System.exit(0);
		}
		Node<E> q=head;


		if(index==0) RemoveFirst();
		else
			for(int i=0;i<index-1;i++)
				q=q.next;
		Node<E> address=q.next;
		
		q.next= address.next;
		if(index==size-1) RemoveLast();
		return address.data;
	}
	public E RemoveFirst(){
		Node p=head; E temp =head.data;
		if(head==null){System.out.println("empty"); System.exit(0);}
		else
			if(size==1) head=tail=null;
			else 
				head=head.next;
		size--; return temp;
	}
	public E RemoveLast(){
		Node<E> p=head;
		for(int i=0;i<size-1;i++){
			p=p.next;
		}
		p.next=null; 
		size=size-1;
		return p.next.data;
	}
	public boolean Contains(E element){
		Node<E> q= head;
		for(int i=0;i<size;i++){
			if(q.data.equals(element))
				return true;
			q=q.next;
		}
		return false;
	}
	public int IndexOf(E element){
		Node<E> q= head;
		for(int i=0;i<size;i++){
			if(q.data.equals(element))
				return i;
			q=q.next;
		}
		return -1;
	}
}
